//
//  ThirdRegisterController.m
//  JF_New_DYKT
//
//  Created by 陈雷 on 2017/8/25.
//  Copyright © 2017年 陈雷. All rights reserved.
//

#import "ThirdRegisterController.h"


@interface ThirdRegisterController ()
@property (weak, nonatomic) IBOutlet UIButton *completeBtn;
@property (weak, nonatomic) IBOutlet BaseTextfiled *firstPsdTf;

@property (weak, nonatomic) IBOutlet BaseTextfiled *secondPsdTf;


@end

@implementation ThirdRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.fromWhere isEqualToString:@"forgetPsd"]) {
        [self.completeBtn setTitle:@"修改完成" forState:UIControlStateNormal];
    }
    
    self.firstPsdTf.keyboardType = UIKeyboardTypeASCIICapable;
     self.secondPsdTf.keyboardType = UIKeyboardTypeASCIICapable;
    [self notificationAction];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setShadowImage:[JFCrazyTool imageWithColor:[UIColor whiteColor]]];
    [self.navigationController.navigationBar setBackgroundImage:[JFCrazyTool imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = YES;
    
}

#pragma mark- 完成操作
- (IBAction)completeAction:(UIButton *)sender {
    
    [self.firstPsdTf resignFirstResponder];
    [self.secondPsdTf resignFirstResponder];
    
    NSLog(@"完成");
    
    if ([self.firstPsdTf.text isEqualToString:self.secondPsdTf.text]) {
        if (self.firstPsdTf.text.length <= 0) {
            [JKToast showWithText:@"请输入密码"];
            return;
        }
    }else{
        [JKToast showWithText:@"两次输入的密码不一致！"];
        return;
    }
    
    self.login_registerModel.password = [JFCrazyTool md5:self.firstPsdTf.text];
    
    
    NSString *registerType = nil;
    NSDictionary *paraDic = nil;
    
    if ([self.fromWhere isEqualToString:@"wx"] || [self.fromWhere isEqualToString:@"qq"] ) {
        registerType = @"user/User/thirdUserRegister";
        paraDic = @{@"mobile":self.login_registerModel.mobile,
                    @"code":self.login_registerModel.code,
                    @"password":self.login_registerModel.password,
                    @"nickname":self.login_registerModel.nickname,
                    @"region":self.login_registerModel.region,
                    @"image_base64":self.login_registerModel.image_base64,
                    @"sex":self.login_registerModel.sex,
                    @"open_id":self.login_registerModel.open_id,
                    @"third_type":self.login_registerModel.third_type};
    }else if ([self.fromWhere isEqualToString:@"forgetPsd"]){
        registerType = @"user/User/resetPwd";
        paraDic = @{@"mobile":self.login_registerModel.mobile,
                    @"code":self.login_registerModel.code,
                    @"password":self.login_registerModel.password};
    }else{
        registerType = @"user/User/userRegister";
        paraDic = @{@"mobile":self.login_registerModel.mobile,
                    @"code":self.login_registerModel.code,
                    @"password":self.login_registerModel.password,
                    @"nickname":self.login_registerModel.nickname,
                    @"region":self.login_registerModel.region,
                    @"image_base64":self.login_registerModel.image_base64,
                    @"sex":self.login_registerModel.sex};
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kDomainName,registerType];
    
    BaseRequest *request =  [BaseRequest sharedBaseRequest];
    [request post:urlStr params:paraDic success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"1"] ) {
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"register" object:nil userInfo:nil];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"forgetPsd" object:nil userInfo:nil];
            
            if (![self.fromWhere isEqualToString:@"forgetPsd"]) {
                
                NSString *userId = responseObject[@"data"][@"UID"];
                [UserDefaults setObject:userId forKey:@"UID"];
                
                [[NSNotificationCenter defaultCenter]postNotificationName:@"dismissAction" object:nil userInfo:nil];
            }
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            
            [JKToast showWithText:responseObject[@"message"]];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}


- (void)notificationAction{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyBoardWillShow:(NSNotification *) note {
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    // 获取键盘高度
    CGRect keyBoardBounds  = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardBounds.size.height;
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.view.transform = CGAffineTransformMakeTranslation(0, -keyBoardHeight+80);
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
    
}

- (void)keyBoardWillHide:(NSNotification *) note {
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.view.transform = CGAffineTransformIdentity;
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
}



@end
